<template>
  <XtxDialog title="添加收货地址" v-model:visible="dialogVisible">
    <template #main>
      <div class="address-edit">
        <div class="xtx-form">
        <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input class="input" placeholder="请输入收货人" v-model="fromData.receiver"/>
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input class="input" placeholder="请输入手机号" v-model="fromData.contact"/>
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity placeholder="请选择所在地区" :fullLocation="fromData.fullLocation" @change="changeCty"/>
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input class="input" placeholder="请输入详细地址" v-model="fromData.address"/>
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input class="input" placeholder="请输入邮政编码" v-model="fromData.postalCode" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input class="input" placeholder="请输入地址标签，逗号分隔" v-model="fromData.addressTags" />
          </div>
        </div>
      </div>
    </div>
    </template>
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-right:20px" @click="dialogVisible= false">取消</XtxButton>
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { ref, reactive } from 'vue'
import { addAddress, addAddressEdit } from '@/api/order'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup (props, { emit }) {
    const dialogVisible = ref(false)
    // 打开函数
    const open = (address) => {
      dialogVisible.value = true
      if (address.id) {
        // 如果修改，填充菜单
        for (const key in address) {
          fromData[key] = address[key]
        }
      } else {
        // 如果添加，清空菜单
        for (const key in fromData) {
          // 判断是否是默认地址
          if (key === 'isDefault') {
            fromData[key] = 1
          } else {
            fromData[key] = null
          }
        }
      }
    }

    // 表单数据
    const fromData = reactive({
      receiver: '',
      contact: '',
      provinceCode: '',
      cityCode: '',
      countyCode: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 0,
      fullLocation: ''
    })

    // 选择地区
    const changeCty = (data) => {
      fromData.provinceCode = data.provinceCode
      fromData.cityCode = data.cityCode
      fromData.countyCode = data.countyCode
      fromData.fullLocation = data.fullLocation
    }

    // 提交请求
    const submit = () => {
      // 1.省略了校验
      // 2.发送请求
      console.log(fromData.id)
      if (fromData.id) {
        // 修改请求
        addAddressEdit(fromData).then(data => {
          Message({ type: 'success', text: '修改收货地址成功' })
          // 关闭弹窗
          dialogVisible.value = false
          emit('on-success', fromData)
        })
      } else {
        addAddress(fromData).then(data => {
          // 设置id
          fromData.id = data.result.id
          // 提示
          Message({ type: 'success', text: '添加收货地址成功' })

          // 关闭弹窗
          dialogVisible.value = false
          emit('on-success', fromData)
        })
      }
    }
    return { dialogVisible, open, fromData, changeCty, submit }
  }
}
</script>
<style scoped lang="less">

.xtx-dialog {
  :deep(.wrapper){
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
